package com.java2104.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.java2104.entity.LoginUser;
import com.java2104.entity.UserMessage;
import com.java2104.mapper.LoginUserMapper;
import com.java2104.mapper.UserMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Service
public class LoginUserService implements IService<LoginUser> {

    @Autowired
    private LoginUserMapper loginUserMapper;

    @Override
    public BaseMapper<LoginUser> getBaseMapper() {
        return loginUserMapper;
    }

    public LoginUser login(String loginName,String password){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(loginName!=null && !"".equals(loginName.trim())){
            queryWrapper.eq("login_name",loginName);
            LoginUser loginUser = loginUserMapper.selectOne(queryWrapper);
            if(loginUser!=null){
                if(loginUser.getPassword().equals(password) && loginUser.getLoginStatus().equals(2)){
                    return loginUser;
                }
            }
        }
        return null;
    }

    public Map modifyPassword(LoginUser loginUser,String oldPass,String newPass,String newPassCon){
        //判断旧密码是否正确
        Map map = new HashMap();
        if(oldPass!=null && oldPass.equals(loginUser.getPassword())){
            //旧密码正确
            if(newPass.equals(newPassCon)){
                loginUser.setPassword(newPass);//设置新密码
                updateById(loginUser);
                map.put("ret_status","success");
                map.put("ret_msg","密码修改成功");
            }else {
                map.put("ret_status","fail");
                map.put("ret_msg","新密码与确认密码不一致");
            }
        }else {
            //旧密码错误
            map.put("ret_status","fail");
            map.put("ret_msg","旧密码输入不正确");
        }
        return map;
    }

    public int saveLoginUser(LoginUser loginUser){
        return loginUserMapper.insert(loginUser);
    }

    @Override
    public boolean saveBatch(Collection<LoginUser> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<LoginUser> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<LoginUser> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(LoginUser entity) {
        return false;
    }

    @Override
    public LoginUser getOne(Wrapper<LoginUser> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<LoginUser> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<LoginUser> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public Class<LoginUser> getEntityClass() {
        return null;
    }
}
